﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;
using Extensions;
using OTS;

namespace OTS
{
    public partial class Pivot : XForm
    {
        public Pivot()
        {
            InitializeComponent();
        }

        private void Pivot_Load(object sender, EventArgs e)
        {
            //return;
        
            //this.panelControl1.Visible = false;
            //bs = new BindingSource(ds, "xOrdine");
            RefreshData();
            ExpandCollapse(splitContainerControl1, AcKapa.Collapse);

            pvg.OptionsPrint.PrintColumnHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.PrintDataHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.PrintFilterHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.MergeRowFieldValues = false;
            pvg.OptionsPrint.MergeColumnFieldValues = false;

            splitContainerControl1.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
        }

        BindingSource bs;
        void pivotGridControl1_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }


        public override void RefreshData()
        {
            bs = gridView1.Init("GetData",1,new DateTime(2000,1,1));

            //DrillDownGrid(gridView1, bs);
            //gridView1.AddSummary("Qty");
            //gridView1.OptionsBehavior.Editable = false;

            InitPivot(pvg, bs);

            //GetDetails("xOrdine", "Get7cOrdine", CKBatchId);
            //gridView1.Columns["idx"].Visible = 
            //gridView1.Columns["CodCliente"].Visible = false;
            pvg.Cells.Selection = new Rectangle(new Point(0, 0), new Size(1, 1));
            //pvg.Fields["ProductId"].BestFit();
            pvg.BestFit();

        }


        bool PivotInitialized = false;
        void InitPivot(PivotGridControl pgc, BindingSource bs)
        {
            pgc.DataSource = bs;

            if (!PivotInitialized)
            {
                pgc.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
                //pgc.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
                pgc.CellSelectionChanged += new EventHandler(pgc_CellSelectionChanged);
                pgc.FocusedCellChanged += new EventHandler(pgc_FocusedCellChanged);
                pgc.CustomAppearance += new PivotCustomAppearanceEventHandler(pivotGridControl1_CustomAppearance);


                pgc.BeginUpdate();

                //new field - toplam tutar  (metre x fiyat+dvz cinsi)

                //AddNewField(pgc, "DosyaNo", PivotArea.FilterArea);
                //AddNewField(pgc, "CodCliente", PivotArea.FilterArea);
                //AddNewField(pgc, "Linea", PivotArea.FilterArea);
                /*
                PivotGridFieldBase[] pgf1 = new PivotGridFieldBase[3];
                pgf1[0] = AddNewField(pgc, "Linea", PivotArea.RowArea);
                pgf1[1] = AddNewField(pgc, "Modello", PivotArea.RowArea);
                pgf1[1].ExpandedInFieldsGroup = false;
                pgf1[2] = AddNewField(pgc, "Tex", PivotArea.RowArea);
                pgc.Groups.Add(pgf1, "ModelloTex");
                */

                //PivotGridField qa = AddNewField(pgc, "EsdegerTl", PivotArea.RowArea);
                //qa.ValueFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                //qa.ValueFormat.FormatString = "#,###.0";
                //qa.Appearance.Value.Options.UseTextOptions = true;
                //qa.Appearance.Value.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;

                //AddNewField(pgc, "ProductId", PivotArea.RowArea).Visible = false;   //.FieldEdit = RepositoryManager.Get(Rp.V_Kumas);
                AddNewField(pgc, "Sirket", PivotArea.ColumnArea);


                PivotGridField[] fields2 = new PivotGridField[3];
                //pivotGridControl1.Fields["Tarih"].ValueFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                //pivotGridControl1.Fields["Tarih"].ValueFormat.FormatString = "d";

                //PivotGridField fieldYear = new PivotGridField("EfektifGun", PivotArea.ColumnArea);
                PivotGridField fieldMonth = new PivotGridField("EfektifGun", PivotArea.RowArea);
                PivotGridField fieldWeek = new PivotGridField("EfektifGun", PivotArea.RowArea);
                PivotGridField fieldDay = new PivotGridField("EfektifGun", PivotArea.RowArea);

                // Add the fields to the field collection.
                pgc.Fields.Add(fieldMonth);
                fieldMonth.GroupInterval = PivotGroupInterval.DateMonth;
                fieldMonth.Caption = "Ay";

                pgc.Fields.Add(fieldWeek);
                fieldWeek.GroupInterval = PivotGroupInterval.DateWeekOfYear;
                fieldWeek.Caption = "Hafta";

                pgc.Fields.Add(fieldDay);
                fieldDay.GroupInterval = PivotGroupInterval.DateDay;
                fieldDay.Caption = "Gün";

                fields2[0] = fieldMonth;
                fields2[1] = fieldWeek;
                fields2[2] = fieldDay;

                //fields2[2].ExpandedInFieldsGroup = fields2[3].ExpandedInFieldsGroup = false;
                pgc.Groups.Add(fields2, "EfektifGun");
                //AddNewField(pgc, "EfektifGun", PivotArea.ColumnArea);

                AddNewField(pgc, "EsdegerTl", PivotArea.DataArea).ValueFormat.FormatString = "#,###.0";
                AddNewField(pgc, "TutarTl", PivotArea.DataArea).ValueFormat.FormatString = "#,###.0";
                AddNewField(pgc, "TutarUsd", PivotArea.DataArea).ValueFormat.FormatString = "#,###.0";
                AddNewField(pgc, "TutarEur", PivotArea.DataArea).ValueFormat.FormatString = "#,###.0";


                pgc.EndUpdate();

                PivotInitialized = true;
            }
        }


        void pgc_FocusedCellChanged(object sender, EventArgs e)
        {
            CellChanged();
        }


        void pgc_CellSelectionChanged(object sender, EventArgs e)
        {
            CellChanged();
        }

        private void CellChanged()
        {
            double PivotSum = 0.0;

            if (pvg.Cells.MultiSelection.SelectedCells.Count > 0)
            {
                foreach (Point cell in pvg.Cells.MultiSelection.SelectedCells)
                {
                    DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pvg.Cells.GetCellInfo(cell.X, cell.Y);
                    if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                        cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                        PivotSum += Convert.ToDouble(pvg.GetCellValue(cell.X, cell.Y));
                }
            }
            else
            {
                DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pvg.Cells.GetFocusedCellInfo();
                if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                    cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                    PivotSum += Convert.ToDouble(pvg.GetCellValue(pvg.Cells.FocusedCell.X, pvg.Cells.FocusedCell.Y));

            }
            //Notify(PivotSum == 0 ? string.Empty : String.Format("{0:#,##0.## mt'.'}", PivotSum));
        }

        void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            gridControl1.DataSource = e.CreateDrillDownDataSource();
            gridView1.MakeColumnInvisible("Name");
            ExpandCollapse(splitContainerControl1, (gridView1.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            gridView1.BestFitColumns();
            gridView1.MoveFirst();
        }
    }
}
